# _*_ coding utf-8 _*_
# FILENAME：versionList.py
# DESCRIPTION: 版本管理
# AUTH: jarvis zhang
# DATE: 2022/5/3110:53 上午

from PyQt5.QtWidgets import *
from PyQt5.Qt import *

from models.versionModel import VersionModel
from models.baseModel import query, delete
from versionListUI import VersionListUI
from versionForm import VersionForm


class VersionList(QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.ui = VersionListUI()
        self.ui.setupUi(self)
        # self.ui.btn_upload.clicked.connect(self.upload_click)
        self.ui.btn_add.clicked.connect(self.add_click)
        # self.ui.btn_edit.clicked.connect(self.form_click)
        # self.ui.btn_del.clicked.connect(self.del_click)
        self.ui.btn_close.clicked.connect(self.Close)
        self.init_data()

    # 初始化数据
    def init_data(self):
        # 添加数据1
        self.query_data_list()


    # 打开窗体
    def Open(self):
        self.show()

    # 关闭窗体
    def Close(self):
        self.close()

    # 将数据操作的按钮绑定到该槽函数
    def add_click(self):
        # print('add')
        VersionForm.get_dialog(self)

    # 将数据操作的按钮绑定到该槽函数
    def edit_click(self):
        # print('edit')
        button = self.sender()
        if button:
            row = self.ui.data_table.indexAt(button.parent().pos()).row()
            id = int(self.ui.data_table.item(row, 1).text())
            name = self.ui.data_table.item(row, 2).text()
            VersionForm.get_dialog(self, id=id, name=name)

    # 删除
    def del_click(self):
        # print('delete')
        button = self.sender()
        if button:
            row = self.ui.data_table.indexAt(button.parent().pos()).row()
            id = int(self.ui.data_table.item(row, 1).text())
            model = VersionModel()
            model.id = id
            delete(model)
            # 刷新列表
            self.query_data_list()

    # 将查询数据的按钮绑定到该槽函数
    def query_data_list(self):
        print('query')
        model = VersionModel()
        data = query(model)
        if len(data) != 0:
            self.ui.data_table.setRowCount(len(data))
            for i in range(len(data)):
                newItem = QTableWidgetItem(str(i + 1))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.ui.data_table.setItem(i, 0, newItem)

                newItem = QTableWidgetItem(str(data[i].id))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.ui.data_table.setItem(i, 1, newItem)

                newItem = QTableWidgetItem(str(data[i].name))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.ui.data_table.setItem(i, 2, newItem)

                newItem = QTableWidgetItem(str(data[i].create_date))
                newItem.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
                self.ui.data_table.setItem(i, 3, newItem)

                #
                # 编辑
                self.btnEdit = QPushButton('编辑')
                self.btnEdit.setStyleSheet(''' text-align : center;
                                                                          background-color : NavajoWhite;
                                                                          height : 30px;
                                                                                  font : 13px  ''')
                self.btnEdit.clicked.connect(self.edit_click)
                # 删除
                self.btnDel = QPushButton('删除')
                self.btnDel.setStyleSheet(''' text-align : center;
                                                                                  background-color : LightCoral;
                                                                                  height : 30px;
                                                                                  font : 13px  ''')
                self.btnDel.clicked.connect(self.del_click)

                widget = QWidget()
                widget.setFixedHeight(30)
                hLayout = QHBoxLayout()
                hLayout.addWidget(self.btnEdit)
                hLayout.addWidget(self.btnDel)
                widget.setLayout(hLayout)
                self.ui.data_table.setCellWidget(i, 4, widget)


if __name__ == "__main__":
    import sys
    app = QApplication(sys.argv)
    MainWindow = VersionList()
    MainWindow.show()
    sys.exit(app.exec_())